package lesson19;

import java.util.Scanner;

public class SimpleNotepad implements INotepad
{
	private Page[] pages;
	
	public SimpleNotepad(int numberOfPages, Scanner sc) 
	{
		if(numberOfPages > 0)
		{
			this.pages = new Page[numberOfPages];
			for (int i = 0; i < pages.length; i++) 
			{
				System.out.println("Enter heading of page number " + (i+1) + ":");
				String title = sc.nextLine();
				pages[i] = new Page(title);
			}
		}
		else
		{
			System.out.println("Number of pages is wrong. Please try again!");
		}
	}
	
	public Page getPage(int numberOfPage)
	{
		if(numberOfPage <= this.pages.length)
		{
			return this.pages[numberOfPage - 1];
		}
		else
		{
			return null;
		}
	}

	@Override
	public void addTextToPage(final int numberOfPage, final String text) 
	{
		if(numberOfPage <= this.pages.length)
		{
			this.pages[numberOfPage - 1].addText(text);
		}
		else
		{
			System.out.println("No such page! Please try again!");
		}
	}

	@Override
	public void changeTextOfPage(int numberOfPage, String text) 
	{
		if(numberOfPage <= this.pages.length)
		{
			this.pages[numberOfPage - 1].deleteText();
			this.pages[numberOfPage - 1].addText(text);
		}
		else
		{
			System.out.println("No such page! Please try again!");
		}
	}

	@Override
	public void deleteTextFromPage(Page selectedPage) 
	{
		for (int i = 0; i < pages.length; i++) 
		{
			if(selectedPage.equals(pages[i]))
			{
				pages[i].deleteText();
			}
		}
	}

	@Override
	public void printAllPages() 
	{
		for (int i = 0; i < pages.length; i++) 
		{
			System.out.println(this.pages[i].getText());
		}
	}

}
